<template>
  <div class="login-wrap">
    <div class="login">
      <h4 class="login-text">账号注册</h4>
      <Form ref="form" :model="user" label-position="left" :rules="rules">
        <FormItem prop="username">
          <Input v-model="user.username">
            <Icon type="ios-contact" slot="prefix" />
          </Input>
        </FormItem>
        <FormItem prop="password">
          <Input type="password" v-model="user.password">
            <Icon type="md-lock" slot="prefix" />
          </Input>
        </FormItem>
        <FormItem prop="email">
          <Input v-model="user.email">
            <Icon type="md-mail" slot="prefix" />
          </Input>
        </FormItem>
        <!-- <FormItem>
          记住密码：
          <i-switch v-model="isSavePassword" />
        </FormItem>-->
        <FormItem>
          <Button type="primary" long @click="register">注册</Button>
        </FormItem>
      </Form>
    </div>
  </div>
</template>

<script>
import { register } from "@/api/profile";

export default {
  data() {
    return {
      user: {
        user: "",
        password: "",
        email: ""
      },
      rules: {
        username: [
          {
            required: true,
            message: "用户名不能为空"
          }
        ],
        password: [
          {
            required: true,
            message: "密码不能为空"
          }
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱"
          },
          {
            type: "email",
            message: "请输入正确的邮箱"
          }
        ]
      }
    };
  },
  methods: {
    register() {
      this.$refs.form.validate(res => {
        if (!res) {
          return;
        }
        register(this.user)
          .then(res => {
            this.$router.push("/login");
          })
          .catch(error => {
            this.remoteMessage = "用户名或密码错误";
          });
      });
    }
  }
};
</script>

<style lang="less" scoped>
.login-wrap {
  display: flex;
  justify-content: center;
  flex-direction: column;
  margin: auto;
  height: 100vh;
  width: 300px;
}
.login {
  border-radius: 5px;
  box-shadow: 0 0 5px #ccc;
  padding: 14px;
}
.login-text {
  text-align: center;
  padding-bottom: 12px;
}

.login-bottom {
  display: flex;
  justify-content: space-between;
}
</style>
